package com.open.rbac.service.id.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;

import com.open.center.common.util.CopyUtils;
import com.open.rbac.model.dto.id.IdentityRelDto;
import com.open.rbac.model.entity.id.IdentityRel;
import com.open.rbac.model.vo.id.IdentityRelVo;
import com.open.rbac.service.id.IdentityRelService;
import com.open.rbac.service.id.repository.IdentityRelRepository;

/**
 * Id关系服务
 *
 * @author Riche's
 * @since 2025/9/5
 */
@RequiredArgsConstructor
@Service
public class IdentityRelServiceImpl implements IdentityRelService {

    private final IdentityRelRepository identityRelRepository;

    @Transactional
    @Override
    public void create(List<IdentityRelVo> relVos) {
        List<IdentityRel> rels = relVos.stream().map(relVo -> {
            IdentityRel rel = new IdentityRel();
            rel.setIdentityId(relVo.getIdentityId());
            rel.setRelIdentityId(relVo.getRelIdentityId());
            return rel;
        }).toList();
        identityRelRepository.saveAll(rels);
    }

    @Transactional
    @Override
    public void deleteReallyByIdentityId(String identityId) {
        identityRelRepository.deleteReallyByIdentityId(identityId);
    }

    @Override
    public void deleteReallyByIdentityIds(List<String> identityIds) {
        identityRelRepository.deleteReallyByIdentityIds(identityIds);
    }

    @Transactional
    @Override
    public void deleteReallyByRelIdentityId(String relIdentityId) {
        identityRelRepository.deleteReallyByRelIdentityId(relIdentityId);
    }

    @Override
    public void deleteReallyByRelIdentityId(String identityId, String relIdentityId) {
        identityRelRepository.deleteReallyByRelIdentityId(identityId, relIdentityId);
    }

    @Override
    public List<IdentityRelDto> listByIdentityIds(List<String> identityIds) {
        return CopyUtils.listCopy(identityRelRepository.findByIdentityIds(identityIds), IdentityRelDto.class);
    }
}
